package com.example.sfzreader.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.util.Log;

public class UsbReceiver extends BroadcastReceiver {

    private static final String TAG = "UsbReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        UsbDevice device = intent.getParcelableExtra("device");
        String usbType = intent.getAction();
        if ("android.hardware.usb.action.USB_DEVICE_ATTACHED".equals(usbType)) {
            if ((intent.getBooleanExtra("permission", false)) && (device != null)) {
                Log.d(TAG, "UsbDevice (VID:" + device.getVendorId() + ",PID:" + device.getProductId() + ") DEVICE_ATTACHED");
            }
        } else if ("android.hardware.usb.action.USB_DEVICE_DETACHED".equals(usbType)) {
            if ((intent.getBooleanExtra("permission", false)) && (device != null)) {
                Log.d(TAG, "UsbDevice (VID:" + device.getVendorId() + ",PID:" + device.getProductId() + ") DEVICE_DETACHED");
            }
        } else if ("com.android.example.USB_PERMISSION".equals(usbType) || "com.hdos.usbdevice.UsbDeviceLib.USB_PERMISSION".equals(usbType)) {
            synchronized (this) {
                if ((intent.getBooleanExtra("permission", false)) && (device != null)) {
                    Log.d(TAG, "permission request get" + device);
                } else {
                    Log.d(TAG, "permission denied for device " + device);
                }
            }
        }
//			switch (usbType) {
//			//USB设备插入
//			case "android.hardware.usb.action.USB_DEVICE_ATTACHED":
//
//				break;
//			//USB设备拔出
//			case "android.hardware.usb.action.USB_DEVICE_DETACHED":
//
//				break;
//			case "com.android.example.USB_PERMISSION":
//			case "com.hdos.usbdevice.UsbDeviceLib.USB_PERMISSION":
//				synchronized (this) {
//					if ((intent.getBooleanExtra("permission", false)) && (device != null)) {
//						Log.d(TAG, "permission request get" + device);
//					} else {
//						Log.d(TAG, "permission denied for device " + device);
//					}
//				}
//				break;
//			}
    }
}
